const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 食品召回信息-- 查询
router.post("/recall", (req, res) => {
    const { values, pageConfig, doName } = req.body;
    const { page, pageSize } = pageConfig
    const sql = values === null ? "select * from recall order by Id limit ?,?;select * from recall" :
        "select * from recall where reFoName=? or reTime=? limit ?,?;select * from recall where reFoName=? or reTime=?"
    const data = values === null ? [(Number(page) - 1) * Number(pageSize), Number(pageSize)] :
        [values.fname, values.fTime,(Number(page) - 1) * Number(pageSize), Number(pageSize), values.fname,values.fTime]
db.sqlQuery(sql, data, (err, result) => {
    if (err) {
        console.log(err)
        res.send({
            // data: result,
            msg: "获取信息失败",
            status: 203
        })
    } else {
        res.send({
            data: result[0],
            total: result[1].length,
            msg: "获取信息成功",
            status: 200
        })
        db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "食品召回信息查询", time.getData()])
    }
})
})

// 食品召回信息-- 添加
router.post("/recall/add", (req, res) => {
    const { reFoName, reSpecs, reNum, reSource, reReason, reStatus, rePlan, reTime,moId, doName } = req.body
    let sql = "insert into recall(reFoName,reSpecs,reNum,reSource,reReason,reStatus,rePlan,reTime,moId) values(?,?,?,?,?,?,?,?,?)"
    const data = [reFoName, reSpecs, reNum, reSource, reReason, reStatus, rePlan, reTime,moId]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            if (err.errno !== 1064) {
                res.send({
                    msg: "添加失败",
                    status: 203
                })
            } else if (err.errno === 1064) {
                res.send({
                    msg: "添加失败,不能为空!",
                    status: 203
                })
            }
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "食品召回信息添加", time.getData()])
        }
    })
})

// 食品召回信息-- 编辑
router.post("/recall/edit", (req, res) => {
    const { reFoName, reSpecs, reNum, reSource, reReason, reStatus, rePlan, reTime,moId, Id, doName } = req.body
    let sql = "update recall set reFoName=?,reSpecs=?,reNum=?,reSource=?,reReason=?,reStatus=?,rePlan=?,reTime=?,moId=? where Id=?"
    const data = [reFoName, reSpecs, reNum, reSource, reReason, reStatus, rePlan, reTime,moId, Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "食品召回信息编辑", time.getData()])
        }
    })
})

// 食品召回信息-- 删除
router.post("/recall/delete", (req, res) => {
    const { Id, doName } = req.body
    let sql = "delete from recall where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)", [doName, "食品召回信息删除", time.getData()])
        }
    })
})

module.exports = router